#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   sos-tar.sh
# Version:    1.0
# Date:       2014/09/24
# Author:     jinzhao.wang      
# Email:      jinzhao.wang@cs2c.com.cn
# Summary:    02系统基本功能-02系统管理功能-16系统和系统服务管理-03系统状态收集>工具sos-02查看收集的系统信息
# Notes:      sosreport命令
# Copyright:  China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2014/09/24
#             -   The first one
#             Version 1.1, 2014/11/24
#             -   The second one;
#             -   Add SFROOT;
#             -   change judge root;
#             -   Add If there is a sosreport file;
# ----------------------------------------------------------------------
if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Management_Function/system_service_management
fi

. ${SFROOT}/lib/Echo.sh
. ${SFROOT}/lib/XmlParse.sh


LANG=en_US.UTF-8

echo $LANG

function CleanData ()
{
  rm -rf /tmp/`hostname |awk -F. '{print $1}'`-*
  rm -rf /tmp/sosreport*
  EchoInfo "sos-tar.sh执行完毕"
}

trap "CleanData" EXIT INT

#确定系统已安装sos软件包
rpm -q sos
EchoResult "系统安装了sos软件包"

#判断root用户
[ "x$(id -ru)" = "x0" ]
EchoResult "判断root用户"

#判断/tmp目录是否存在sosreport文件
if [ -f /tmp/sosreport-*.tar.xz ];then
    rm -rf /tmp/sosreport-*.tar.xz
    EchoInfo "/tmp目录下不存在sosreport"
else
    EchoInfo "/tmp目录下不存在sosreport"
fi


#sosreport收集系统信息
/usr/bin/expect <<-EOF
set timeout -1
spawn sosreport 
    expect "*quit."
    send "\r"
    expect "*last name *:"
    send "\r"    
    expect "* report for *:"
    send "1\r"    
    expect eof
EOF
echo "..."
[ -f /tmp/sosreport-*.tar.xz ]
EchoResult "成功收集到系统信息"

tar -xvf /tmp/sosreport*.tar.xz -C /tmp

host=`hostname |awk -F. '{print $1}'`
[ -d /tmp/${host}-* ]
EchoResult "成功解压"
cat /tmp/${host}-*/df
EchoResult "df文件正常显示"


